Rename events_order to events_list_order and readd events_order

`events_order` determines how to select events for outputting, whereas
`events_list_order` determines how to list selected events in the
output.

Akinori MUSHA 9 ans auparavant
Parent
Commettre
978ec06199

+ 19 - 5
app/models/agents/data_output_agent.rb

@@ -45,9 +45,14 @@ module Agents
45 45
               "_contents": "tag contents (can be an object for nesting)"
46 46
             }
47 47
 
48
-        # Ordering events in the output
48
+        # Ordering events
49 49
 
50
-        #{description_events_order('events in the output')}
50
+        #{description_events_order('events')}
51
+
52
+        DataOutputAgent will select the last `events_to_show` entries of its received events sorted in the order specified by `events_order`, which is defaulted to the event creation time.
53
+        So, if you have multiple source agents that may create many events in a run, you may want to either increase `events_to_show` to have a larger "window", or specify the `events_order` option to an appropriate value (like `date_published`) so events from various sources are properly mixed in the resulted feed.
54
+
55
+        There is also an option `events_list_order` to control the order of events listed in the output, with the same format as `events_order`.  It is defaulted to `#{Utils.jsonify(DEFAULT_EVENTS_ORDER['events_list_order'])}` so the latest entry is listed first.
51 56
 
52 57
         # Liquid Templating
53 58
 
@@ -175,7 +180,16 @@ module Agents
175 180
       interpolated['push_hubs'].presence || []
176 181
     end
177 182
 
178
-    def sorted_events(reload = false)
183
+    DEFAULT_EVENTS_ORDER = {
184
+      'events_order' => nil,
185
+      'events_list_order' => [["{{_index_}}", "number", true]],
186
+    }
187
+
188
+    def events_order(key = SortableEvents::EVENTS_ORDER_KEY)
189
+      super || DEFAULT_EVENTS_ORDER[key]
190
+    end
191
+
192
+    def latest_events(reload = false)
179 193
       events =
180 194
         if (event_ids = memory[:event_ids]) &&
181 195
            memory[:events_order] == events_order &&
@@ -229,7 +243,7 @@ module Agents
229 243
         end
230 244
       end
231 245
 
232
-      source_events = sorted_events()
246
+      source_events = sort_events(latest_events(), 'events_list_order')
233 247
 
234 248
       interpolation_context.stack do
235 249
         interpolation_context['events'] = source_events
@@ -297,7 +311,7 @@ module Agents
297 311
       url = feed_url(secret: interpolated['secrets'].first, format: :xml)
298 312
 
299 313
       # Reload new events and update cache
300
-      sorted_events(true)
314
+      latest_events(true)
301 315
 
302 316
       push_hubs.each do |hub|
303 317
         push_to_hub(hub, url)

+ 25 - 0
db/migrate/20160607055850_change_events_order_to_events_list_order.rb

@@ -0,0 +1,25 @@
1
+class ChangeEventsOrderToEventsListOrder < ActiveRecord::Migration
2
+  def up
3
+    Agents::DataOutputAgent.find_each do |agent|
4
+      if value = agent.options.delete('events_order')
5
+        agent.options['events_list_order'] = value
6
+        agent.save!(validate: false)
7
+      end
8
+    end
9
+  end
10
+
11
+  def down
12
+    Agents::DataOutputAgent.transaction do
13
+      Agents::DataOutputAgent.find_each do |agent|
14
+        if agent.options['events_order']
15
+          raise ActiveRecord::IrreversibleMigration, "Cannot revert migration because events_order is configured"
16
+        end
17
+
18
+        if value = agent.options.delete('events_list_order')
19
+          agent.options['events_order'] = value
20
+          agent.save!(validate: false)
21
+        end
22
+      end
23
+    end
24
+  end
25
+end

+ 7 - 7
spec/models/agents/data_output_agent_spec.rb

@@ -142,7 +142,7 @@ describe Agents::DataOutputAgent do
142 142
           "url" => "http://imgs.xkcd.com/comics/evolving0.png",
143 143
           "title" => "Evolving yet again with a past date",
144 144
           "date" => '2014/05/05',
145
-          "hovertext" => "Something else"
145
+          "hovertext" => "A small text"
146 146
         }
147 147
       end
148 148
 
@@ -166,7 +166,7 @@ describe Agents::DataOutputAgent do
166 166
 
167 167
            <item>
168 168
             <title>Evolving yet again with a past date</title>
169
-            <description>Secret hovertext: Something else</description>
169
+            <description>Secret hovertext: A small text</description>
170 170
             <link>http://imgs.xkcd.com/comics/evolving0.png</link>
171 171
             <pubDate>#{Time.zone.parse(event3.payload['date']).rfc2822}</pubDate>
172 172
             <guid isPermaLink="false">#{event3.id}</guid>
@@ -216,7 +216,7 @@ describe Agents::DataOutputAgent do
216 216
           'items' => [
217 217
             {
218 218
               'title' => 'Evolving yet again with a past date',
219
-              'description' => 'Secret hovertext: Something else',
219
+              'description' => 'Secret hovertext: A small text',
220 220
               'link' => 'http://imgs.xkcd.com/comics/evolving0.png',
221 221
               'guid' => {"contents" => event3.id, "isPermaLink" => "false"},
222 222
               'pubDate' => Time.zone.parse(event3.payload['date']).rfc2822,
@@ -244,20 +244,20 @@ describe Agents::DataOutputAgent do
244 244
 
245 245
       describe 'ordering' do
246 246
         before do
247
-          agent.options['events_order'] = ['{{title}}']
247
+          agent.options['events_order'] = ['{{hovertext}}']
248
+          agent.options['events_list_order'] = ['{{title}}']
248 249
         end
249 250
 
250 251
         it 'can reorder the events_to_show last events based on a Liquid expression' do
251
-          # Check that ordering takes place before limiting, not after
252 252
           agent.options['events_to_show'] = 2
253 253
           asc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
254
-          expect(asc_content['items'].map {|i| i["title"] }).to eq(["Evolving again", "Evolving yet again with a past date"])
254
+          expect(asc_content['items'].map {|i| i["title"] }).to eq(["Evolving", "Evolving again"])
255 255
 
256 256
           agent.options['events_to_show'] = 40
257 257
           asc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
258 258
           expect(asc_content['items'].map {|i| i["title"] }).to eq(["Evolving", "Evolving again", "Evolving yet again with a past date"])
259 259
 
260
-          agent.options['events_order'] = [['{{title}}', 'string', true]]
260
+          agent.options['events_list_order'] = [['{{title}}', 'string', true]]
261 261
 
262 262
           desc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
263 263
           expect(desc_content['items']).to eq(asc_content['items'].reverse)